import fetch from 'utils/fetch';

// 上传
export function upload(data) {
  return fetch({
    url: '/common/upload/',
    method: 'post',
    data
  })
}

export function getCollection(data) {
  return fetch({
    url: '/common/collection/',
    method: 'get',
    params: data
  });
}

export function isCollected(data) {
  return fetch({
    url: '/common/is_collected/',
    method: 'get',
    params: data
  });
}

// 获取地址
export function getAddress(city) {
  return fetch({
    url: '/common/city/',
    method: 'get',
    params: city
  })
}

// 发送验证码
export function sendCode(data) {
  return fetch({
    url: '/common/get_captcha/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 验证验证码是否正确
export function checkCode(data) {
  return fetch({
    url: '/common/verify_captcha/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 提交试卷
export function submitPaper(data) {
  return fetch({
    url: '/paper/submit/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 提交试题
export function submitTest(data) {
  return fetch({
    url: '/paper/submit/exer/',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 试一试
export function getPaper(data) {
  return fetch({
    url: '/paper/practice/',
    method: 'get',
    params: data
  })
}

// 检查试卷是否被购买
export function checkPaperBuy(data) {
  return fetch({
    url: '/paper/isbuy/',
    method: 'get',
    params: data
  })
}